#!/usr/bin/perl
#this will automatically find out the location of this script
use Cwd 'abs_path';
#line var contains path to this script
$line = abs_path($0);
chomp $line;
@DR_array = split('/',$line);
pop(@DR_array);
pop(@DR_array);
$dir = join("/",@DR_array);

#package will help to read config paramater
use Getopt::Long;
&Getopt::Long::GetOptions(
'config=s'      => \$config
);

#this script will help to read the config file parameters
require "$dir/bin/CONFIG.pl";
getDetails($config);

#all the parameters mentioned in the config file are now copied to local variables
my $input_direc = $config{"INPUT_FOLDER"};
my $output_direc = $config{"OUTPUT_FOLDER"};
#print "INPUT DIREC:$input_direc\tOUTPUT DIREC:$output_direc\tTEMP DIREC:$dirtemp\tERROR FILE:$file_error\tSAMPLE QC:$file_sampleqc\n";
print "INPUT DIREC:$input_direc\tOUTPUT DIREC:$output_direc\n";
chomp $input_direc;
chomp $output_direc;
#checking if parameters are empty
if($input_direc eq "")
{
	die "entered input directory is empty\n";
	
}
if($output_direc eq "")
{
        die "entered output directory is empty\n";

}
#creating the temp and outputdirectories if not exists
if (!(-d $output_direc )) 
{
	system("mkdir -p $output_direc");
}
if (!(-d $input_direc )) {
	print "There is a no input directory!";
}

system("mkdir -p $output_direc");
$input_direc =~ s/\/$//;
$output_direc =~ s/\/$//;
$sys = "/usr/local/biotools/r/R-2.12.2/bin/Rscript $dir/bin/MethylationProcessBoraBRCAExample.R $input_direc $output_direc $dir/bin/GPL8490_HumanMethylation27_270596_v.1.2.csv";
#die "$sys\n";
system($sys);
$k=`ls $output_direc`;
if($k eq "")
{
        die "Rscript failed\n";
}

open ERROR,">$output_direc/sample.error";
print ERROR "ID_TYPE\tID\tSEVERITY\tMSG_CODE\tDETAILS\n";
close(ERROR);
#print "sucess\n";
